package com.twu.biblioteca;

import java.util.Scanner;

public class OptionReserve implements Option {

    Scanner scanner;
    Collection collection;

    public OptionReserve(Collection collection, Scanner scanner) {
        this.collection = collection;
        this.scanner = scanner;
    }

    @Override
    public Object run(User user) {

        if (user == null) {
            printNoUserReserveFailure();
            return null;
        }

        printReserveRequest();
        Book bookSelected = getSelectedBook();

        if (bookSelected.isReserved()) {
            printReserveFailure();
        }
        else {
            bookSelected.setReserved();
            printReserveSuccess();
        }

        return null;
    }

    private void printNoUserReserveFailure() {
        System.out.println("You must be logged in to reserve a book.");
    }

    private void printReserveRequest() {
        System.out.println("Enter the number of the book you wish to reserve:");
    }

    private Book getSelectedBook() {
        return collection.getBookFromList(readSelectionIndex() - 1);
    }

    private int readSelectionIndex() {
        return Integer.parseInt(scanner.nextLine());
    }

    private void printReserveFailure() {
        System.out.println("Sorry we don't have that book yet.");
    }

    private void printReserveSuccess() {
        System.out.println("Thank You! Enjoy the book.");
    }

    public String menuDescription() {
        return "Reserve a book";
    }
}
